package com.demo.FileOutputStream_;

import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author gy
 * @date 2022/4/4
 */
public class FileOutputStream01 {
    private static final Logger logger = Logger.getLogger(FileOutputStream01.class);

    @Test
    public void m1() {
        String filePath = "f:\\20220403\\write_new1.txt";

        FileOutputStream fileOutputStream = null;
//        byte[] aByte = new byte[8];
//        int read = 0;
        try {
            logger.info("创建文件输出流对象");
            fileOutputStream = new FileOutputStream(filePath);
//           采用追加的方式，将内容追加到文件的末尾
            fileOutputStream = new FileOutputStream(filePath,true);
//            logger.info("开始往 write_new1.txt 写入文件");
//            fileOutputStream.write('H');
//            logger.info("写入 完成");

            String s = "hello,world";
//            fileOutputStream.write(s.getBytes("UTF-8"));

            fileOutputStream.write(s.getBytes(),0,s.length()-5);

        } catch (IOException e) {
            logger.error(e.getMessage());
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
